package test.heliotrope.net.loader
{	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.media.SoundChannel;
	
	import flexunit.framework.Assert;
	import flexunit.framework.TestCase;
	
	import heliotrope.net.loader.BitmapTarget;
	import heliotrope.net.loader.CommonLoader;
	import heliotrope.net.loader.LoadMission;
	import heliotrope.net.loader.LoadType;
	import heliotrope.net.loader.SoundTarget;
	import heliotrope.net.loader.TextTarget;
	
	import org.flexunit.async.Async;
	import org.fluint.uiImpersonation.UIImpersonator;
	
	
	public class CommonLoaderTest
	{		
		private static var __cm:CommonLoader;
		[Before]
		public function setUp():void
		{
			__cm = new CommonLoader(9);
		}
		
		[After]
		public function tearDown():void
		{
		}
		
		[BeforeClass]
		public static function setUpBeforeClass():void
		{
		}
		
		protected static function getStage(event:Event):void
		{
		}
		
		[AfterClass]
		public static function tearDownAfterClass():void
		{
		}
		
		[Test(async)]
		public function testLoadText():void
		{
			var textTarget:TextTarget = new TextTarget;
			__cm.addEventListener(Event.COMPLETE, Async.asyncHandler(this, textComplete, 1000, textTarget));
			__cm.load(new LoadMission("testdata/net/loader/textTest.txt", textTarget, 	LoadType.TEXT));
		}
		
		protected function textComplete(event:Event, passThroughData_:Object):void
		{
			Assert.assertEquals("test", passThroughData_.text);
		}
		
		[Test(async)]
		public function testLoadPic():void
		{
			var bitmapTarget:BitmapTarget = new BitmapTarget;
			__cm.addEventListener(Event.COMPLETE, Async.asyncHandler(this, picComplete, 1000, bitmapTarget));
			__cm.load(new LoadMission("testdata/net/loader/picTest.jpg", bitmapTarget, LoadType.PIC));
		}
		
		protected function picComplete(event:Event, passThroughData_:Object):void
		{
			Assert.assertNotNull(passThroughData_.bitmapData);
		}
		
		[Test(async)]
		public function testLoadSound():void
		{
			var soundTarget:SoundTarget = new SoundTarget;
			__cm.addEventListener(Event.COMPLETE, Async.asyncHandler(this, soundComplete, 1000, soundTarget));
			__cm.load(new LoadMission("testdata/net/loader/soundTest.mp3", soundTarget, LoadType.SOUND));
		}
		
		protected function soundComplete(event:Event, passThroughData_:Object):void
		{
			var channel:SoundChannel;
			try
			{
				channel = passThroughData_.play();
			} 
			catch(error:Error) 
			{
				Assert.fail(error.message);
				return;
			}
			channel.stop();
		}
		
		[Test]
		public function testToString():void
		{
			Assert.assertEquals("CommonLoader[9]:",__cm.toString());
		}
	}
}